import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2023-03-04
 * Time: 11:39
 */
public class Day19 {
    //13.罗马数字转整数
    public int romanToInt1(String s) {
        HashMap<Character, Integer> map = new HashMap<>();

        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);

        char[] c = s.toCharArray();
        if (c.length == 1){
            return map.get(c[0]);
        }
        int sum = 0;
        for (int i = 0; i < c.length; i++) {
            int a = map.get(c[i]);
            if (i + 1 < c.length && a < map.get(c[i + 1])){
                sum -= a;
            }else{
                sum += a;
            }
        }

        return sum;
    }
    public int romanToInt(String s) {
        int[] ret = new int[26];

        ret['I' - 'A'] = 1;
        ret['V' - 'A'] = 5;
        ret['X' - 'A'] = 10;
        ret['L' - 'A'] = 50;
        ret['C' - 'A'] = 100;
        ret['D' - 'A'] = 500;
        ret['M' - 'A'] = 1000;

        char[] c = s.toCharArray();
        if (c.length == 1){
            return ret[c[0] - 'A'];
        }
        int sum = 0;
        for (int i = 0; i < c.length; i++) {
            if (i + 1 < c.length && ret[c[i] - 'A'] < ret[c[i + 1] - 'A']){
                sum -= ret[c[i] - 'A'];
            }else{
                sum += ret[c[i] - 'A'];
            }
        }

        return sum;
    }
    //202.快乐数 -- 标记
    //浑水摸鱼
    public boolean isHappy1(int n) {
        int sum = 0;

        while (n != 1) {
            while (n > 0) {
                int ret = n % 10;
                sum = sum + (int)Math.pow(ret, 2);
                n /= 10;
            }
            if (sum == 4) {
                return false;
            }
            n = sum;
            sum = 0;
        }
        return true;
    }
    //哈希表
    private int getSum(int n){
        int sum = 0;
        while (n > 0){
            int ret = n % 10;
            sum += ret * ret;
            n /= 10;
        }

        return sum;
    }
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)){
            set.add(n);
            n = getSum(n);
        }
        return n == 1;
    }
    //242.有效的字母异位词
    public boolean isAnagram(String s, String t) {
        char[] ss = s.toCharArray();
        char[] ts = t.toCharArray();

        if (ss.length != ts.length){
            return false;
        }
        int[] si = new int[26];
        for (int i = 0; i < ss.length; i++) {
            si[ss[i] - 'a']++;
        }
        for (int i = 0; i < ts.length; i++) {
            si[ts[i] - 'a']--;
        }
        for (int i = 0; i < si.length; i++) {
            if (si[i] != 0){
                return false;
            }
        }
        return true;
    }
    //290.单词规律
    public static boolean wordPattern(String pattern, String s) {
        String[] ss = s.split(" ");
        char[] ps = pattern.toCharArray();
        if (ss.length != ps.length){
            return false;
        }

        Map<Character, String> map1 = new HashMap<>();
        Map<String, Character> map2 = new HashMap<>();
        for (int i = 0; i < ps.length; i++) {
            if (map1.containsKey(ps[i]) && !map1.get(ps[i]).equals(ss[i])){
                return false;
            }
            if (map2.containsKey(ss[i]) && !map2.get(ss[i]).equals(ps[i])){
                return false;
            }
            map1.put(ps[i], ss[i]);
            map2.put(ss[i], ps[i]);
        }
        return true;
    }
    //383.赎金信
    public boolean canConstruct(String ransomNote, String magazine) {
        char[] rs = ransomNote.toCharArray();
        char[] ms = magazine.toCharArray();

        if (rs.length > ms.length){
            return false;
        }
        int[] ret = new int[26];
        for (int i = 0; i < ms.length; i++) {
            ret[ms[i] - 'a']++;
        }
        for (int i = 0; i < rs.length; i++) {
            ret[rs[i] - 'a']--;
            if (ret[rs[i] - 'a'] < 0){
                return false;
            }
        }
        return true;
    }
}
